package com.aiwiown.snackmq.console.web;

import com.aiwiown.snackmq.common.dto.TopicInfo;
import com.aiwiown.snackmq.console.service.SnackMQAdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 【新增】处理所有与 Topic 相关的 Web 请求的控制器。
 */
@Slf4j
@Controller
@RequestMapping("/topics")
public class TopicController {

    @Autowired
    private SnackMQAdminService adminService;

    @GetMapping
    public String listTopics(Model model) {
        try {
            model.addAttribute("topics", adminService.listTopics());
        } catch (Exception e) {
            log.error("Failed to list topics", e);
            model.addAttribute("errorMessage", "Failed to list topics: " + e.getMessage());
        }
        return "topics";
    }

    @GetMapping("/{topicName}")
    public String topicDetails(@PathVariable String topicName, Model model) {
        try {
            TopicInfo topicInfo = adminService.getTopicDetails(topicName);
            if (topicInfo == null) {
                model.addAttribute("error", "Topic '" + topicName + "' not found.");
            }
            model.addAttribute("topic", topicInfo);
        } catch (Exception e) {
            log.error("Failed to get details for topic '{}'", topicName, e);
            model.addAttribute("error", "Failed to get topic details: " + e.getMessage());
        }
        return "topic-detail";
    }

    @PostMapping("/create")
    public String createTopic(@RequestParam String topicName,
                              @RequestParam int partitions,
                              @RequestParam int replicationFactor,
                              RedirectAttributes redirectAttributes) {
        try {
            adminService.createTopic(topicName, partitions, replicationFactor);
            redirectAttributes.addFlashAttribute("successMessage", "Topic '" + topicName + "' created successfully.");
        } catch (Exception e) {
            log.error("Failed to create topic '{}'", topicName, e);
            redirectAttributes.addFlashAttribute("errorMessage", "Failed to create topic: " + e.getMessage());
        }
        return "redirect:/topics";
    }

    @PostMapping("/delete")
    public String deleteTopic(@RequestParam String topicName, RedirectAttributes redirectAttributes) {
        try {
            adminService.deleteTopic(topicName);
            redirectAttributes.addFlashAttribute("successMessage", "Topic '" + topicName + "' deleted successfully.");
        } catch (Exception e) {
            log.error("Failed to delete topic '{}'", topicName, e);
            redirectAttributes.addFlashAttribute("errorMessage", "Failed to delete topic: " + e.getMessage());
        }
        return "redirect:/topics";
    }
}